/**
 * Project Name: jxcommons
 * Package Name: org.jxcommons.lang.comparator
 * created on 22.11.2012 by jkonopka
 */
package org.jenkon.comparator;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.jenkon.assertion.Assert;

/**
 * composite comparator class. class is for compare many {@link Comparator} with
 * each other.
 * 
 * @author Jens Konopka
 * 
 * @param <T> type of comparable object
 */
public class CompositeComparator<T> implements Comparator<T>, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8129622217017869148L;

	/**
	 * {@link List} with delegating {@link Comparator}.
	 */
	private final List<Comparator<T>> delegates;

	/**
	 * @param delegates List with delegating {@link Comparator} objects
	 */
	public CompositeComparator(final Comparator<T>... delegates) {
		this(Arrays.asList(delegates));
	}

	/**
	 * @param delegates {@link List} with delegating {@link Comparator} objects
	 */
	public CompositeComparator(final List<Comparator<T>> delegates) {
		Assert.isNotNull(delegates, "");
		this.delegates = delegates;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(final T lho, final T rho) {
		int value = 0;
		for (final Comparator<T> delegate : delegates) {
			value = delegate.compare(lho, rho);
			if (value != 0) {
				break;
			}
		}
		return value;
	}

}
